package com.qf.fmmall.service.impl;

import com.qf.fmmall.dao.*;
import com.qf.fmmall.entity.*;
import com.qf.fmmall.service.ProductService;
import com.qf.fmmall.vo.ResStatus;
import com.qf.fmmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductImgMapper productImgMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductParamsMapper productParamsMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo listRecommends() {
        ResultVo vo = new ResultVo();

        List<ProductVo> productVoList=productMapper.listRecommends();

        if (productVoList.size()==0){
            vo.setCode(ResStatus.NO);
            vo.setMsg("没有查询到商品数据");
        }else {
            vo.setCode(ResStatus.OK);
            vo.setMsg("success");
            vo.setData(productVoList);
        }
        return vo;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo categoryRecommends() {
        ResultVo vo = new ResultVo();

        List<CategoryVo> categoryVoList=categoryMapper.categoryRecommends();
        if (categoryVoList.size()==0){
            vo.setCode(ResStatus.NO);
            vo.setMsg("没有查询到商品分类数据");
        }else {
            vo.setCode(ResStatus.OK);
            vo.setMsg("success");
            vo.setData(categoryVoList);
        }
        return vo;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo detailInfo(Integer productId) {
        ResultVo vo = new ResultVo();

        Product product = productMapper.selectByPrimaryKey(productId);

        if (product==null){
            vo.setCode(ResStatus.NO);
            vo.setMsg("没有查询到该商品信息");
        }else {
            Example example = new Example(ProductImg.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("itemId",productId);
            List<ProductImg> productImgs = productImgMapper.selectByExample(example);

            Example example1 = new Example(ProductSku.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("productId",productId);
            List<ProductSku> productSkus = productSkuMapper.selectByExample(example1);


            HashMap<String, Object> map = new HashMap<>();

            map.put("product",product);
            map.put("productImgs",productImgs);
            map.put("productSkus",productSkus);
            vo.setCode(ResStatus.OK);
            vo.setMsg("success");
            vo.setData(map);
        }
        return vo;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo detailParams(Integer productId) {
        ResultVo vo = new ResultVo();

        ProductParams condition = new ProductParams();
        condition.setProductId(productId+"");
        ProductParams productParams = productParamsMapper.selectOne(condition);

        if (productParams==null){
            vo.setCode(ResStatus.NO);
            vo.setMsg("没有查询到该商品的参数信息");
        }else {
            vo.setCode(ResStatus.OK);
            vo.setMsg("success");
            vo.setData(productParams);
        }

        return vo;
    }
}
